local SettingHelp = {}

SettingHelp.VERY_LOW = 0
SettingHelp.LOW = 1
SettingHelp.MIDDLE = 2
SettingHelp.HIGH = 3
SettingHelp.DEFAULT = SettingHelp.LOW

local DeviceMemoryLevel = nil
local DeviceCpuLevel = nil
-- 内存等级
function SettingHelp:getMemoryLevel()
    if DeviceMemoryLevel then
        return DeviceMemoryLevel
    end
    local platform = cc.Application:getInstance():getTargetPlatform()
  
    local settingLevel = SettingHelp.DEFAULT
    
    if platform == cc.PLATFORM_OS_IPHONE or platform == cc.PLATFORM_OS_IPAD then
        local memory = device.getTotalMemory()
        release_print("cur device total memory: ", memory )
        string.gsub( memory, "^%s*([%d%.,]*).-%s*(%a*)%s*$", function(num, unit)
            num = string.gsub(num or "" , ",", ".")
            num = tonumber(num) or 0
            if unit == "GB" then
                if num <= 1 then
                    -- 1g以下
                    settingLevel = SettingHelp.LOW
                elseif num <=2 then
                    -- 2g以下
                    settingLevel = SettingHelp.MIDDLE
                else
                    settingLevel = SettingHelp.HIGH
                end
            else
                settingLevel = SettingHelp.LOW
            end
        end)
    elseif platform == cc.PLATFORM_OS_ANDROID then
        local memory = device.getTotalMemoryBytes()
        release_print(string.format("cur device total memory: %skb", memory ))
        local GBUnit = 1024 * 1024
        if memory > 0  then
            local gb = memory/GBUnit
            if gb < 1.5 then
                settingLevel = SettingHelp.VERY_LOW
            elseif gb < 2 then
                settingLevel = SettingHelp.LOW
            elseif gb <= 3.8 then
                 -- 设备误差 
                settingLevel = SettingHelp.MIDDLE
            else
                settingLevel = SettingHelp.HIGH
            end
        end

        -- string.gsub( memory, "^%s*([%d%.,]*).-%s*(%a*)%s*$", function(num, unit)
        --     num = string.gsub(num or "", ",", ".")
        --     num = tonumber(num) or 0
        --     if unit == "GB" then
        --         if num <= 1.5 then
        --             settingLevel = SettingHelp.VERY_LOW
        --         elseif num <= 2 then
        --             --2g 以下 含2g
        --             settingLevel = SettingHelp.LOW
        --         elseif num <=4 then
        --             -- 2g - 4g 
        --             settingLevel = SettingHelp.MIDDLE
        --         else
        --             -- 4g 以上
        --             settingLevel = SettingHelp.HIGH
        --         end
        --     else
        --         settingLevel = SettingHelp.LOW
        --     end
        -- end)
    else
        settingLevel = SettingHelp.HIGH
    end
    DeviceMemoryLevel = settingLevel
    return settingLevel
end

--cpu 等级
function SettingHelp:getCpuLevel()
    if DeviceCpuLevel then
        return DeviceCpuLevel
    end
    local settingLevel = SettingHelp.DEFAULT
    local platform = cc.Application:getInstance():getTargetPlatform()
    if platform == cc.PLATFORM_OS_IPHONE or platform == cc.PLATFORM_OS_IPAD then
        local deviceName = device.getSystemHardware()
        release_print("======> get device name : ", deviceName)
        string.gsub(deviceName,'^(%a*)(%d*)%s-,%s-(%d*)',function(tag, mainV, childV)
            mainV = tonumber(mainV) or 0
            childV = tonumber(childV) or 0
            if tag == 'iPhone' then
                if mainV < 8 then
                    --iphone4
                    --5 5c
                    --6
                    settingLevel = SettingHelp.LOW
                elseif mainV <= 9 then
                    -- 6s 6splus 7 7plus
                    settingLevel = SettingHelp.MIDDLE
                else
                    --7 以上
                    settingLevel = SettingHelp.HIGH
                end
            elseif tag == 'iPod' then
                if mainV < 6 then -- touch 5
                    settingLevel = SettingHelp.LOW
                elseif mainV == 6 then
                    settingLevel = SettingHelp.MIDDLE
                end
            elseif tag == 'iPad' then
                if mainV < 4 then --iPad Air
                    --iPad 4
                    settingLevel = SettingHelp.LOW
                elseif mainV == 4 and childV < 7 then --iPad Mini 3
                    settingLevel = SettingHelp.MIDDLE
                else
                    settingLevel = SettingHelp.HIGH
                end
            end
        end)
    elseif platform == cc.PLATFORM_OS_ANDROID then
        -- local cpuFreqStr = device.getMaxCpuFreq()
        -- local cupNum = device.getCPUNumCores()
        local cpuHardware = device.getCpuHardware() or "||"
        local cpuHardwareArr = string.split(cpuHardware, "|")
        local cpuFreqStr = cpuHardwareArr[2] or ""
        local cupNum = tonumber(cpuHardwareArr[3]) or -1
        
        local cpuFreq  = -1
        if cpuFreqStr ~= "N/A" then
            local _, _,  freqStr = string.find(cpuFreqStr, "^%s*(%d*)%s*KHz%s*$")
            cpuFreq = tonumber(freqStr) or -1
        end
        release_print("======> getCpuHardware  : ", cpuHardware, cpuFreqStr, cupNum, cpuFreq)
        if cpuFreq > 0 then
            if cpuFreq < 2000000 then
                settingLevel = SettingHelp.LOW
            elseif cpuFreq < 2400000 then
                settingLevel = SettingHelp.MIDDLE
            else
                settingLevel = SettingHelp.HIGH
            end
        end
    else
        settingLevel = SettingHelp.HIGH
    end
    DeviceCpuLevel = settingLevel
    return settingLevel;
end

function SettingHelp.getCpuInfo()
    local platform = cc.Application:getInstance():getTargetPlatform()
    if  platform == cc.PLATFORM_OS_ANDROID then
        local cpuHardware = device.getCpuHardware() or "||"
        local cpuHardwareArr = string.split(cpuHardware, "|")
        local cpuArchStr = cpuHardwareArr[1] or ""
        local cpuFreqStr = cpuHardwareArr[2] or ""
        local cupNum = tonumber(cpuHardwareArr[3]) or -1
        local cpuFreq  = -1
        if cpuFreqStr ~= "N/A" then
            local _, _,  freqStr = string.find(cpuFreqStr, "^%s*(%d*)%s*KHz%s*$")
            cpuFreq = tonumber(freqStr) or -1
        end
        release_print("======> getCpuHardware  : ", cpuHardware, cpuArchStr, cpuFreqStr, cupNum, cpuFreq)
        return cpuFreq, cpuArchStr
    end
end

function SettingHelp.getDefaultSetting()    
    local configs = {}
    local memoryLevel = SettingHelp.getMemoryLevel()
    local cpuLevel = SettingHelp.getCpuLevel()

    release_print(string.format("cur memory level %d, cur cpu level %d", memoryLevel, cpuLevel))
    if memoryLevel == SettingHelp.HIGH and cpuLevel == SettingHelp.HIGH then
        configs.showPlayerLevel = "most"
        configs.autoPlayWeather = true
    elseif memoryLevel >= SettingHelp.MIDDLE and cpuLevel >= SettingHelp.MIDDLE then
        configs.showPlayerLevel = "normal"
        configs.autoPlayWeather = true
    else
        configs.showPlayerLevel = "little"
        configs.autoPlayWeather = false
    end

    configs.showPlayerUphead = cpuLevel >= SettingHelp.MIDDLE
    configs.isFastFps = true --cpuLevel == SettingHelp.HIGH

    configs.isTextureHalfSize = (cpuLevel <= SettingHelp.MIDDLE) or (memoryLevel <= SettingHelp.MIDDLE)

    --战斗 角色描边
    configs.showBattleOutline = cpuLevel == SettingHelp.HIGH

    --特效等级
    configs.isEffectAllShow = (cpuLevel > SettingHelp.LOW) and (memoryLevel > SettingHelp.LOW)

    --战斗内其他设置
    configs.showBattlePalHp = configs.isEffectAllShow
    configs.showBattlePalName = configs.isEffectAllShow
    configs.showBattleFoeHp = configs.isEffectAllShow
    configs.showBattleFoeName = configs.isEffectAllShow
    configs.showBattleChat = configs.isEffectAllShow
    -- 
    configs.genBattleStaticBgEnable = not configs.isEffectAllShow
    
    return configs
end

return SettingHelp